package com.public_module.tools;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

//字符串处理
public class StringProcess {
    //判断字符串是否 不为空且长度大于0
    public static boolean stringIsZero(String string) {
        if (string == null || string.length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    //判断字符串长度是否大于size
    public static boolean stringMoreThan(String string, int size) {
        if (string != null && string.length() > size) {
            return true;
        } else {
            return false;
        }
    }

    //判断字符串长度是否小于size
    public static boolean stringLessThan(String string, int size) {
        if (string != null && string.length() < size) {
            return true;
        } else {
            return false;
        }
    }

    //判断字符串长度是否等于size
    public static boolean stringEquals(String string, int size) {
        if (string != null && string.length() == size) {
            return true;
        } else {
            return false;
        }
    }

    //判断手机号
    public static boolean isPhone(String phone) {
        //String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        String regex = "(1)\\d{10}$";
        if (phone == null || phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    //手机号隐藏中间4位
    public static String phoneHide(String phone) {
        if (phone == null || phone.length() < 11) {
            return "";
        }
        String str = "****";
        StringBuilder sb = new StringBuilder(phone);
        sb.replace(3, 7, str);
        return sb.toString();
    }

    public static String responsetoString(ResponseBody responseBody) {
        BufferedSource source = responseBody.source();
        Buffer buffer = source.buffer();
        Charset charset = Charset.forName("UTF-8");
        return buffer.clone().readString(charset);
    }

    public static SpannableString stringForegroundColorSpan(String string, int pos, String color) {
        SpannableString spannableString = new SpannableString(string);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(color));
        spannableString.setSpan(colorSpan, pos, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static List deepCopy(List src) throws IOException, ClassNotFoundException
    {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        return (List)in.readObject();
    }

    public static Long getTimestamp(String time) {
        Long timestamp = null;
        try {
            timestamp = new SimpleDateFormat("HH:mm:ss").parse(time).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timestamp;
    }

    public static String codeMat(String string) {
        String res = "";
        try {
            if(string.contains("/")){
                int a = string.lastIndexOf("/");
                res = string.substring(a+1);
            }else if(string.contains("=")){
                int a = string.lastIndexOf("=");
                res = string.substring(a+1);
            }else {
                res = string;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return  res;
        }
    }
}
